package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.pojo.Dish;
import com.itheima.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Set;

/**
 * 菜品管理
 */
@Slf4j
@RestController
@RequestMapping("dish")
public class DishController {

    @Autowired
    private DishService dishService;
    @Autowired
    private RedisTemplate redisTemplate;


    /**
     * 保存菜品
     */
    @PostMapping
    @CacheEvict(cacheNames = "dish", allEntries = true)
    public R<String> save(@RequestBody DishDto dishDto) {
        //保存菜品信息，不仅包括菜品，还包含口味集合
        dishService.saveWithDishFlavor(dishDto);
        return R.success("添加成功");
    }

    /**
     * 分页查询
     */
    @GetMapping("page")
    public R<Page<DishDto>> page(Integer page, Integer pageSize, String name) {
        //调用业务层
        Page<DishDto> pageInfo = dishService.pageDishDto(page, pageSize, name);
        return R.success(pageInfo);
    }

    /**
     * 根据ic查询菜品（包含口味集合）
     */
    @GetMapping("{id}")
    public R<DishDto> getById(@PathVariable Long id) {
        DishDto dishDto = dishService.getByIdWithFlavor(id);
        return R.success(dishDto);
    }


    /**
     * 根据id修改菜品信息
     */
    @PutMapping
    @CacheEvict(cacheNames = "dish", allEntries = true)
    public R<String> update(@RequestBody DishDto dishDto) {
        dishService.updateWithFlavor(dishDto);
        return R.success("修改成功");
    }


    /**
     * 启售、停售状态
     */
    @PostMapping("status/{status}")
    @CacheEvict(cacheNames = "dish", allEntries = true)
    public R<String> updateStatus(@PathVariable Integer status, Long[] ids) {
        // update dish set status=? where id in ( )
        dishService.lambdaUpdate() //lambdaUpdate表示可以做修改，删除操作
                .in(Dish::getId, Arrays.asList(ids)) //更新条件
                .set(Dish::getStatus, status) //修改字段
                .update(); //修改操作
        return R.success("修改成功");
    }

    /**
     * 批量删除
     */
    @DeleteMapping
    @CacheEvict(cacheNames = "dish", allEntries = true)
    public R<String> deleteByIds(Long[] ids) {
        dishService.deleteByIds(ids);
        return R.success("删除成功");
    }


    /**
     * 根据条件查询菜品列表
     */
    @GetMapping("list")
    //@Cacheable(cacheNames = "dish", key = "#dish.categoryId + ':' + #dish.name")
    @Cacheable(cacheNames = "dish", key = "T(org.apache.commons.codec.digest.DigestUtils).md5Hex(#dish.toString())")
    public R<List<DishDto>> list(Dish dish) {
        //select * from dish where name like ? or category_id=?
        /*List<Dish> list = dishService.lambdaQuery()
                .eq(Dish::getStatus, 1)
                .like(StringUtils.isNotBlank(name), Dish::getName, name)
                .eq(categoryId != null, Dish::getCategoryId, categoryId)
                .list();*/

        List<DishDto> list = dishService.listDishDto(dish);

        return R.success(list);
    }
}
